package com.library.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.library.bean.Admin;
import com.library.dao.AdminDao;
import com.library.dao.BaseDao;

public class AdminDaoImpl implements AdminDao {

	@Override
	public Admin selectAdmin(String adminName, String passWord) {
		Admin admin = null;
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "SELECT * FROM ADMIN WHERE ADMINNAME = ? AND PASSWORD = ?";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setString(1, adminName);
			ps.setString(2, passWord);
			rs = ps.executeQuery();
			if(rs.next()) {
				admin = new Admin();
				admin.setAdminId(rs.getInt("adminId"));
				admin.setAdminName(rs.getString("adminName"));
				admin.setPassWord(rs.getString("passWord"));
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			BaseDao.closeAll(rs, ps, con);
		}
		return admin;
	}

	@Override
	public Admin selectAdminByadminName(String adminName) {
		Admin admin = null;
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "SELECT * FROM ADMIN WHERE ADMINNAME = ?";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setString(1, adminName);
			rs = ps.executeQuery();
			if(rs.next()) {
				admin = new Admin();
				admin.setAdminId(rs.getInt("adminId"));
				admin.setAdminName(rs.getString("adminName"));
				admin.setPassWord(rs.getString("passWord"));
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			BaseDao.closeAll(rs, ps, con);
		}
		return admin;
	}

}
